package com.ayf.payment.game.api.service.platform.impl;

import com.ayf.payment.game.api.dto.MerchantDTO;
import com.ayf.payment.game.api.entity.platform.WxConfig;
import com.ayf.payment.game.api.entity.platform.WxQrCode;
import com.ayf.payment.game.api.mapper.platform.WxQrCodeMapper;
import com.ayf.payment.game.api.service.platform.WxConfigService;
import com.ayf.payment.game.api.service.platform.WxQrCodeService;
import com.ayf.payment.game.api.type.LoginType;
import com.ayf.payment.game.api.wx.MyConstant;
import com.ayf.payment.game.api.wx.WxHttpUtil;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class WxQrCodeServiceImpl implements WxQrCodeService {

    @Resource
    private WxQrCodeMapper wxQrCodeMapper;
    @Autowired
    private WxConfigService wxConfigService;
    @Autowired
    private MyConstant myConstant;

    @Override
    public WxQrCode getQrCodeByMerchantId(MerchantDTO merchantDTO) {

        String loginType = merchantDTO.getLoginType();
        int a;
        if (LoginType.FATHER.name().equals(loginType)) {
            a = 1;
        } else if (LoginType.SON.name().equals(loginType)) {
            a = 2;
        } else {
            a = 3;
        }

        WxQrCode wxQrCode = new WxQrCode();
        wxQrCode.setMerchnatId(merchantDTO.getId());
        wxQrCode.setLoginId(merchantDTO.getLoginId());
        WxQrCode dbQrCode = wxQrCodeMapper.selectOne(wxQrCode);
        String validTime = "2592000";
        String key = "BIND;" + merchantDTO.getLoginAccount() + ";" + merchantDTO.getId() + ";" + merchantDTO.getLoginId() + ";" + a;
        if (dbQrCode == null) {
            dbQrCode = new WxQrCode();
            dbQrCode.setCreateTime(String.valueOf(System.currentTimeMillis() / 1000));
            dbQrCode.setMerchnatId(merchantDTO.getId());
            dbQrCode.setLoginId(merchantDTO.getLoginId());
            dbQrCode.setValidTime(validTime);
            dbQrCode.setTicket(getTicket(key,key, validTime));
            wxQrCodeMapper.insertSelective(dbQrCode);
        } else {
            long nowTime = System.currentTimeMillis() / 1000;
            long dbCreateTime = Integer.parseInt(dbQrCode.getCreateTime());
            long dbValidTime = Integer.parseInt(dbQrCode.getValidTime());
            if ((nowTime - dbCreateTime) > dbValidTime) {
                dbQrCode.setCreateTime(String.valueOf(System.currentTimeMillis() / 1000));
                dbQrCode.setValidTime(validTime);
                dbQrCode.setTicket(getTicket(key,key, validTime));
                wxQrCodeMapper.updateByPrimaryKeySelective(dbQrCode);
            }
        }
        return dbQrCode;
    }


    private String getTicket(String traceId,Object sceneValue, String validTime) {
        WxConfig config = wxConfigService.getConfig();
        String url ="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + myConstant.getAccessToken(config);
        String data3 = "{\"expire_seconds\": " + validTime + ", \"action_name\": \"QR_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"" + sceneValue + "\"}}}";
        JSONObject jsonObject = WxHttpUtil.doPost(traceId,url, data3);
        return jsonObject.get("ticket").toString();
    }

    @Override
    public String getLoginQrCode(String key) {
        return getTicket(key,key, "300");
    }

}